/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.vacoor.xqq.ui.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ImageObserver;

/**
 * @author Vacoor
 */
public class ImageUtil {
    private static final Logger logger = LoggerFactory.getLogger(ImageUtil.class);
//    private MediaTracker mediaTracker = new MediaTracker(null);
    //图片缩放
    /*
      public static ImageIcon getResizePicture(ImageIcon originalPic, Dimension changedImageDim) {
          AffineTransform transform = AffineTransform.getScaleInstance(0.5, 0.5);// 返回表示缩放变换的变换
          AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
//          dest = op.filter(src, null);
          return null;
      }
      */
    /*
    public static ImageIcon getResizePicture(ImageIcon originalPic, Dimension changedImageDim) {
        if (originalPic == null || changedImageDim == null) {
            return originalPic;
        }
        int changedImageWidth = changedImageDim.width;
        int changedImageHeight = changedImageDim.height;

        // 获得原始图片的宽度。
        int originalImageWidth = originalPic.getIconWidth();
        // 获得原始图片的高度。
        int originalImageHeight = originalPic.getIconHeight();
        if ((originalImageWidth == changedImageWidth && originalImageHeight == changedImageHeight)
                || originalImageWidth <= 0
                || originalImageHeight <= 0) {
            return originalPic;
        }
        BufferedImage oriImage = new BufferedImage(originalImageWidth, originalImageHeight, BufferedImage.TYPE_INT_ARGB);
        //        oriImage.createGraphics().drawImage(originalPic.getImage(), 0, 0, null);

        //将透明替换为白色
        Graphics2D g = oriImage.createGraphics();
//        g.setColor(Color.white);
//        g.fillRect(0, 0, originalImageWidth, originalImageHeight);
        g.drawImage(originalPic.getImage(), 0, 0, null);

        // 生成处理后的图片存储空间。
        BufferedImage changedImage = new BufferedImage(changedImageWidth, changedImageHeight, BufferedImage.TYPE_INT_ARGB);

        double widthBo = (double) changedImageWidth / originalImageWidth;
        double heightBo = (double) changedImageHeight / originalImageHeight;

        AffineTransform transform = new AffineTransform();
        transform.setToScale(widthBo, heightBo);

        // 根据原始图片生成处理后的图片。
        AffineTransformOp ato = new AffineTransformOp(transform, null);
        ato.filter(oriImage, changedImage);
        // 返回处理后的图片
        return new ImageIcon(changedImage);
    }
    */

    /*
    public static BufferedImage toBufferedImage(Image image) {
        if(image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        int width = image.getWidth(null);
        int height = image.getHeight(null);
        if(width < 0 || height < 0) {
            logger.warn("image width/height is zero {}", image);
            return null;
        }

        BufferedImage dest = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics g = dest.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return dest;
    }

    public static BufferedImage resize(Image image, Dimension destDim) {
        return resize(toBufferedImage(image), destDim);
    }
    */

    /**
     * 缩放
     *
     * @param src
     * @param destDim
     * @return
     */
    /*
    public static BufferedImage resize(BufferedImage src, Dimension destDim) {
        int width = src.getWidth();
        int height = src.getHeight();
        if( (width == destDim.width && height == destDim.height) || width < 0 || height < 0) {
            return src;
        }

        // 获取缩放变换
        AffineTransform transform = AffineTransform.getScaleInstance(destDim.width / 1.0 / width, destDim.height / 1.0 / height);
        return new AffineTransformOp(transform, null).filter(src, null);
    }
    */


    // 发现这个缩放效率要高不少, 明显不卡了
    public static Image resize(Image src, Dimension destDim) {
        // 平滑度优先缩放
        return src.getScaledInstance(destDim.width, destDim.height, Image.SCALE_SMOOTH);
    }

    public static Image gray(Image image) {
        return GrayFilter.createDisabledImage(image);
    }

    public static Image drawWatermark(Image image, Image watermark, int x, int y, int width, int height, ImageObserver observer) {
        int w = image.getWidth(observer);
        int h = image.getHeight(observer);
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics g = img.getGraphics();
        g.drawImage(image, 0, 0, w, h, observer);
        g.drawImage(watermark, x, y, width, height, observer);
        g.dispose();
        return img;
    }
    /*
    public static BufferedImage gray(Image image) {
        return gray(toBufferedImage(image));
    }

    /**
     * 灰度化
     * @param image
     * @return
     */
    /*
    public static BufferedImage gray(BufferedImage image) {
        return new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null).filter(image, null);
    }
    */
}
